<?php
require_once __DIR__ . '/../vendor/autoload.php';

use cccmvc\Application;

const APP_ROOT = __DIR__ . '/../app';

$config = require APP_ROOT . '/../config/app.php';

$app = new Application(APP_ROOT, $config);

$app->router->get('/', function () {
    $userid = Application::$app->session->get('userid');
    $data = ['username' => ''];

    if ($userid) {
        $data['username'] = (new \app\models\User($userid))->username;
    }

    return \cccmvc\View::render('index', $data);
});

$app->router->get('/user', [\app\controllers\UserController::class, 'index']);

$app->router->get('/login', [\app\controllers\UserController::class, 'login']);
$app->router->post('/login', [\app\controllers\UserController::class, 'doLogin']);

$app->router->get('/logout', [\app\controllers\UserController::class, 'logout']);

$app->router->get('/register', [\app\controllers\UserController::class, 'register']);
$app->router->post('/register', [\app\controllers\UserController::class, 'doRegister']);

$app->router->get('/test/echo2', [\app\controllers\IndexController::class, 'index']);
try {

    $app->run();
} catch (\Throwable $e) {
    echo "<pre>";
    var_dump($e->getCode());
    var_dump($e->getMessage());

}
